﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using TripsLogic.util;
using TripsLogic.model;

namespace TripsLogic.controller
{
    public class SysUserController
    {
        public static DataTable getUserByEmail(string email)
        {
            //get
            try
            {
                object[,] parameters = { { "@email", email } };
                return DB.getData("select * from SYSUSER where email=@email", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getUserByEmail"));
                return null;
            }
        }

        internal static DataTable getUserById(string id)
        {
            //get
            try
            {
                object[,] parameters = { { "@id", id } };
                return DB.getData("select * from SYSUSER where id=@id", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getUserById"));
                return null;
            }
        }

        internal static bool updateUser(SysUser user)
        {
            //get
            try
            {
                if (user.roleId == -1)
                {
                    object[,] parameters = {   {"@alias", user.alias }, 
                                           {"@fname", user.fname }, 
                                           {"@lname", user.lname }, 
                                           {"@dob", user.DOB }, 
                                           {"@sex", user.sex }, 
                                           {"@phone", user.phone }, 
                                           {"@fax", user.fax }, 
                                           {"@mobile", user.mobile },
                                           {"@id", user.id }};
                    DB.exec("update SysUser set alias = @alias, fname = @fname, lname = @lname, dob = @dob, " +
                        "sex = @sex, phone = @phone, fax = @fax, mobile = @mobile " +
                        "where id = @id;", parameters);
                }
                else
                {
                    object[,] parameters = {   {"@alias", user.alias }, 
                                           {"@fname", user.fname }, 
                                           {"@lname", user.lname }, 
                                           {"@dob", user.DOB }, 
                                           {"@sex", user.sex }, 
                                           {"@phone", user.phone }, 
                                           {"@fax", user.fax }, 
                                           {"@mobile", user.mobile },
                                           {"@roleId", user.roleId },
                                           {"@id", user.id }};
                    DB.exec("update SysUser set alias = @alias, fname = @fname, lname = @lname, dob = @dob, " +
                         "sex = @sex, phone = @phone, fax = @fax, mobile = @mobile, roleId = @roleId " +
                         "where id = @id;", parameters);
                }
               
                return true;
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error updateUser"));
                return false;
            }
        }
    }
}